﻿using System;
using System.Threading;
using Microsoft.Office.Interop.Excel;

namespace Schedule_Generator
{
    public partial class FrmMain //Background Works
    {
        private Thread threadLogin;
        private Thread threadFileExport;

        private void Login()
        {
            StartWaiting();
            string rawHTML = HttpHelper.getHTML(txtID.Text, txtPassword.Text);
            ShowStatus("登录成功，正在分析课程信息...");
            statusBar.Refresh();
            if (!rawHTML.Contains(Course.FLAG_USERNAME))
            {
                ShowStatus("登录失败，请检查用户名/密码", StatusType.Warning);
                EndWaiting();
                grpSave.Enabled = false;
                return;
            }
            _courseCollection = CourseCollection.CreateCourses(rawHTML);
            _dataTable = null;
            _dataTable = new string[TABLE_SIZE_X, TABLE_SIZE_Y];
            _dataTable = CreateTable(_courseCollection, txtFormat.Text);
            grpSave.Enabled = true;
            AcceptButton = btnSave;
            ShowStatus("解析完成，请点击保存到文件");
            EndWaiting();
        }

        private void FileExport(object objTable)
        {
            StartWaiting();
            string[,] table = null;
            try
            {
                table = (string[,])objTable;
            }
            catch (Exception ex)
            {
                ShowStatus("线程传参错误:类型不匹配" + ex.Message, StatusType.Warning);
            }
            if (table == null)
            {
                return;
            }
            Application excelApp = new Application
            {
                DisplayAlerts = false
            };
            Workbooks workbooks = excelApp.Workbooks;
            Workbook workbook = workbooks.Add(XlWBATemplate.xlWBATWorksheet);
            Worksheet worksheet = (Worksheet)workbook.Worksheets[1];
            worksheet.Name = "课程表";
            //首次填充
            for (int i = 0; i < table.GetLength(0); i++)
            {
                for (int j = 0; j < table.GetLength(1); j++)
                {
                    worksheet.Cells[i + 1, j + 1] = table[i, j];
                }
            }
            //合并单节课程
            for (int i = 0; i < table.GetLength(0); i++)
            {
                for (int j = 0; j < table.GetLength(1); j++)
                {
                    string test = table[i, j];
                    if (test == MERGEABLE)
                    {
                        Range rangeToMerge = worksheet.Range[worksheet.Cells[i, j + 1], worksheet.Cells[i + 1, j + 1]];
                        rangeToMerge.Merge();
                    }
                }
            }
            //合并为空的大节课程
            int[] x = { 2, 4, 7, 9, 11, 13 };  //第1,3,5,7,9,11节
            foreach (int i in x)
            {
                for (int j = 1; j < TABLE_SIZE_Y; j++)
                {
                    if (table[i, j] == null && table[i + 1, j] == null)
                    {
                        Range rangeToMerge = worksheet.Range[worksheet.Cells[i + 1, j + 1], worksheet.Cells[i + 2, j + 1]];
                        rangeToMerge.Merge();
                    }
                }
            }
            Range range = excelApp.Range["A1", "H1"]; //标题
            range.Merge();
            range.Font.Bold = true;
            range.Font.Size = 15;
            range.RowHeight = 50;
            range.EntireRow.AutoFit();
            range = excelApp.Range["A2", "H15"]; //内容
            range.Font.Size = 11;
            range = excelApp.Range["A1", "H15"]; //全部
            range.NumberFormatLocal = "@"; //单元格格式为文本
            range.RowHeight = 300;
            range.ColumnWidth = 50;
            range.HorizontalAlignment = XlHAlign.xlHAlignCenter;
            range.VerticalAlignment = XlVAlign.xlVAlignCenter;
            range.EntireRow.AutoFit();
            range.EntireColumn.AutoFit();
            worksheet.UsedRange.Borders.LineStyle = 1;
            Invoke(new MainThreadInvokerDelegate(SaveAs), new object[] { worksheet, workbook, excelApp }
                );
            EndWaiting();
        }


    }

}
